<!DOCTYPE html>
<html lang=zh>
<head>
    <meta charset="utf-8">
    
    <title>告别枯燥的CRUD，Java代码生成器的使用 | IT浮云</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
    <meta name="description" content="代码生成器有很多，这里介绍的是rapid-generator 4.0.6，这个项目近几年已经没有更新，但是对于功能使用毫无影响，而且扩展性很强，可以生成满足各种需求的代码。  本文讲的是springboot+jpa组合，其他组合相信大家看了文章后可以自己去尝试。">
<meta name="keywords" content="-代码生成">
<meta property="og:type" content="article">
<meta property="og:title" content="告别枯燥的CRUD，Java代码生成器的使用">
<meta property="og:url" content="http://www.itfuyun.com/posts/rapid-generator.html">
<meta property="og:site_name" content="IT浮云">
<meta property="og:description" content="代码生成器有很多，这里介绍的是rapid-generator 4.0.6，这个项目近几年已经没有更新，但是对于功能使用毫无影响，而且扩展性很强，可以生成满足各种需求的代码。  本文讲的是springboot+jpa组合，其他组合相信大家看了文章后可以自己去尝试。">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="http://upload-images.jianshu.io/upload_images/6332391-1691bd65da4551c1.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/600">
<meta property="og:image" content="http://upload-images.jianshu.io/upload_images/6332391-aec768ecdf319fc6.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/800">
<meta property="og:image" content="http://upload-images.jianshu.io/upload_images/6332391-2c8a015f1893fdf2.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/600">
<meta property="og:image" content="http://upload-images.jianshu.io/upload_images/6332391-e454eb5b97ee392c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/600">
<meta property="og:image" content="http://upload-images.jianshu.io/upload_images/6332391-cc2e982285a8a649.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/600">
<meta property="og:image" content="http://upload-images.jianshu.io/upload_images/6332391-c41c4b2dfaec595d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/600">
<meta property="og:updated_time" content="2017-10-20T12:55:23.145Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="告别枯燥的CRUD，Java代码生成器的使用">
<meta name="twitter:description" content="代码生成器有很多，这里介绍的是rapid-generator 4.0.6，这个项目近几年已经没有更新，但是对于功能使用毫无影响，而且扩展性很强，可以生成满足各种需求的代码。  本文讲的是springboot+jpa组合，其他组合相信大家看了文章后可以自己去尝试。">
<meta name="twitter:image" content="http://upload-images.jianshu.io/upload_images/6332391-1691bd65da4551c1.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/600">
    

    
        <link rel="alternate" href="/atom.xml" title="IT浮云" type="application/atom+xml" />
    

    
        <link rel="icon" href="/favicon.ico" />
    

    <link rel="stylesheet" href="/libs/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="/libs/open-sans/styles.css">
    <link rel="stylesheet" href="/libs/source-code-pro/styles.css">

    <link rel="stylesheet" href="/css/style.css">

    <script src="/libs/jquery/2.1.3/jquery.min.js"></script>
    
    
        <link rel="stylesheet" href="/libs/lightgallery/css/lightgallery.min.css">
    
    
        <link rel="stylesheet" href="/libs/justified-gallery/justifiedGallery.min.css">
    
    
    
    


</head>

<body>
    <div id="container">
        <header id="header">
    <div id="header-main" class="header-inner">
        <div class="outer">
            <a href="/" id="logo">
                <i class="logo"></i>
                <span class="site-title">IT浮云</span>
            </a>
            <nav id="main-nav">
                
                    <a class="main-nav-link" href="/.">首页</a>
                
                    <a class="main-nav-link" href="/archives">文章</a>
                
                    <a class="main-nav-link" href="/about">关于</a>
                
            </nav>
            
                
                <nav id="sub-nav">
                    <div class="profile" id="profile-nav">
                        <a id="profile-anchor" href="javascript:;">
                            <img class="avatar" src="/css/images/head.jpg" />
                            <i class="fa fa-caret-down"></i>
                        </a>
                    </div>
                </nav>
            
            <div id="search-form-wrap">

    <form class="search-form">
        <input type="text" class="ins-search-input search-form-input" placeholder="搜索" />
        <button type="submit" class="search-form-submit"></button>
    </form>
    <div class="ins-search">
    <div class="ins-search-mask"></div>
    <div class="ins-search-container">
        <div class="ins-input-wrapper">
            <input type="text" class="ins-search-input" placeholder="想要查找什么..." />
            <span class="ins-close ins-selectable"><i class="fa fa-times-circle"></i></span>
        </div>
        <div class="ins-section-wrapper">
            <div class="ins-section-container"></div>
        </div>
    </div>
</div>
<script>
(function (window) {
    var INSIGHT_CONFIG = {
        TRANSLATION: {
            POSTS: '文章',
            PAGES: '页面',
            CATEGORIES: '分类',
            TAGS: '标签',
            UNTITLED: '(未命名)',
        },
        ROOT_URL: '/',
        CONTENT_URL: '/content.json',
    };
    window.INSIGHT_CONFIG = INSIGHT_CONFIG;
})(window);
</script>
<script src="/js/insight.js"></script>

</div>
        </div>
    </div>
    <div id="main-nav-mobile" class="header-sub header-inner">
        <table class="menu outer">
            <tr>
                
                    <td><a class="main-nav-link" href="/.">首页</a></td>
                
                    <td><a class="main-nav-link" href="/archives">文章</a></td>
                
                    <td><a class="main-nav-link" href="/about">关于</a></td>
                
                <td>
                    
    <div class="search-form">
        <input type="text" class="ins-search-input search-form-input" placeholder="搜索" />
    </div>

                </td>
            </tr>
        </table>
    </div>
</header>

        <div class="outer">
            
                

<aside id="profile">
    <div class="inner profile-inner">
        <div class="base-info profile-block">
            <img id="avatar" src="/css/images/head.jpg" />
            <h2 id="name">IT浮云</h2>
            <h3 id="title">非典型程序员</h3>
            <span id="location"><i class="fa fa-map-marker"></i>深圳, 中国</span>
            <a id="follow" target="_blank" href="https://github.com/itfuyun/">关注我</a>
        </div>
        <div class="article-info profile-block">
            <div class="article-info-block">
                6
                <span>文章</span>
            </div>
            <div class="article-info-block">
                7
                <span>标签</span>
            </div>
        </div>
        
        <div class="profile-block social-links">
            <table>
                <tr>
                    
                    
                    <td>
                        <a href="http://github.com/itfuyun/" target="_blank" title="github" class=tooltip>
                            <i class="fa fa-github"></i>
                        </a>
                    </td>
                    
                    <td>
                        <a href="mailto:itfuyun@gmail.com" target="_blank" title="envelope" class=tooltip>
                            <i class="fa fa-envelope"></i>
                        </a>
                    </td>
                    
                    <td>
                        <a href="/" target="_blank" title="weibo" class=tooltip>
                            <i class="fa fa-weibo"></i>
                        </a>
                    </td>
                    
                    <td>
                        <a href="/atom.xml" target="_blank" title="rss" class=tooltip>
                            <i class="fa fa-rss"></i>
                        </a>
                    </td>
                    
                </tr>
            </table>
        </div>
        
    </div>
</aside>

            
            <section id="main"><article id="post-rapid-generator" class="article article-type-post" itemscope itemprop="blogPost">
    <div class="article-inner">
        
        
            <header class="article-header">
                
    
        <h1 class="article-title" itemprop="name">
            告别枯燥的CRUD，Java代码生成器的使用
        </h1>
    

                
                    <div class="article-meta">
                        
    <div class="article-date">
        <i class="fa fa-calendar"></i>
        <a href="/posts/rapid-generator.html">
            <time datetime="2017-09-26T12:49:39.000Z" itemprop="datePublished">2017-09-26</time>
        </a>
    </div>


                        
    <div class="article-category">
    	<i class="fa fa-folder"></i>
        <a class="article-category-link" href="/categories/programming/">编程</a>
    </div>

                        
    <div class="article-tag">
        <i class="fa fa-tag"></i>
        <a class="tag-link" href="/tags/代码生成/">-代码生成</a>
    </div>

                    </div>
                
            </header>
        
        
        <div class="article-entry" itemprop="articleBody">
        
            
                <div id="toc" class="toc-article">
                <strong class="toc-title">文章目录</strong>
                    <ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#准备工作"><span class="toc-number">1.</span> <span class="toc-text">准备工作</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#项目搭建与配置"><span class="toc-number">2.</span> <span class="toc-text">项目搭建与配置</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#建立骨架项目"><span class="toc-number">3.</span> <span class="toc-text">建立骨架项目</span></a></li></ol>
                </div>
            
            <p>代码生成器有很多，这里介绍的是rapid-generator 4.0.6，这个项目近几年已经没有更新，但是对于功能使用毫无影响，而且扩展性很强，可以生成满足各种需求的代码。  本文讲的是springboot+jpa组合，其他组合相信大家看了文章后可以自己去尝试。</p>
<a id="more"></a>
<h1 id="准备工作"><a href="#准备工作" class="headerlink" title="准备工作"></a>准备工作</h1><p>网上有源码下载，这里我主要下了两个东西，一个是<a href="http://maven.outofmemory.cn/com.googlecode.rapid-framework/rapid-generator/" target="_blank" rel="external">rapid-generator</a>（这个是主要源码，必须的），另一个是<a href="http://maven.outofmemory.cn/com.googlecode.rapid-framework/rapid-generator-template/" target="_blank" rel="external">rapid-generator-template</a>（里面有各种模版例子，方便使用），如果不需要修改源码也可以直接maven引入这个源码包。<br>rapid-generator Maven地址<br><figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.googlecode.rapid-framework<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>rapid-generator<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>4.0.6<span class="tag">&lt;/<span class="name">version</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></div></pre></td></tr></table></figure></p>
<p>rapid-generator-templater Maven地址<br><figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.googlecode.rapid-framework<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>rapid-generator-template<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>4.0.6<span class="tag">&lt;/<span class="name">version</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></div></pre></td></tr></table></figure></p>
<p>这个生成器用freemarker来进行生成文件，主要就是通过读取数据库的各种信息，包装成数据，填充到我们的模版中，这样就得到了我们需要的代码文件。  </p>
<h1 id="项目搭建与配置"><a href="#项目搭建与配置" class="headerlink" title="项目搭建与配置"></a>项目搭建与配置</h1><p>在生成代码之前我们还需要来进行搭建项目以及相关配置，新建一个maven项目largh-codegen，将rapid-generator（这里不需要rapid-generator-template，这个稍后用到再讲）jar解压，源码拷贝到项目中，因为这个代码生成器使用freemarker，所以我们在pom.xml引入freemarker依赖<br><figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.freemarker<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>freemarker<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>2.3.23<span class="tag">&lt;/<span class="name">version</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></div></pre></td></tr></table></figure></p>
<p>我用的是mysql，因此再添加数据库驱动<br><figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></div><div class="line">     <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>Mysql<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></div><div class="line">     <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>mysql-connector-java<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></div><div class="line">     <span class="tag">&lt;<span class="name">version</span>&gt;</span>5.1.38<span class="tag">&lt;/<span class="name">version</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></div></pre></td></tr></table></figure></p>
<p>现在就来说下rapid-generator-template，将这个jar包解压，进入rapid-generator-template-4.0.6-sources\generator\template\rapid\share\basic这个目录，可以看到5个include文件，我们找个地方新建一个文件夹template（我直接在本项目resources下新建了template文件夹），把这五个文件放入其中，其实这几个文件就是定义了一些版权、注释等信息，可以自己修改成自己的。在我们新建的项目classpath下新建一个生成配置文件generator.xml，Maven项目就直接放在resources下面即可。<br><figure class="highlight dust"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div><div class="line">83</div><div class="line">84</div><div class="line">85</div><div class="line">86</div><div class="line">87</div><div class="line">88</div></pre></td><td class="code"><pre><div class="line"><span class="xml"><span class="php"><span class="meta">&lt;?</span>xml version=<span class="string">"1.0"</span> encoding=<span class="string">"GBK"</span><span class="meta">?&gt;</span></span></span></div><div class="line"><span class="xml"><span class="meta">&lt;!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"&gt;</span></span></div><div class="line"><span class="xml"><span class="tag">&lt;<span class="name">properties</span>&gt;</span></span></div><div class="line"><span class="xml">    <span class="tag">&lt;<span class="name">comment</span>&gt;</span></span></div><div class="line"><span class="xml">        代码生成器配置文件:</span></div><div class="line"><span class="xml">        1.会为所有的property生成property_dir属性,如pkg=com.company =&gt; pkg_dir=com/company</span></div><div class="line"><span class="xml">        2.可以引用环境变量: $</span><span class="template-variable">&#123;env.JAVA_HOME&#125;</span><span class="xml"> or System.properties: $</span><span class="template-variable">&#123;user.home&#125;</span><span class="xml">,property之间也可以相互引用</span></div><div class="line"><span class="xml">    <span class="tag">&lt;/<span class="name">comment</span>&gt;</span></span></div><div class="line"><span class="xml"></span></div><div class="line"><span class="xml">    <span class="tag">&lt;<span class="name">entry</span> <span class="attr">key</span>=<span class="string">"basepackage"</span>&gt;</span>com.largh<span class="tag">&lt;/<span class="name">entry</span>&gt;</span></span></div><div class="line"><span class="xml"></span></div><div class="line"><span class="xml">    <span class="comment">&lt;!-- jsp namespace: web/$</span></span><span class="template-variable">&#123;namespace&#125;</span><span class="xml"><span class="comment">/$</span></span><span class="template-variable">&#123;className&#125;</span><span class="xml"><span class="comment">/list.jsp --&gt;</span></span></div><div class="line"><span class="xml">    <span class="tag">&lt;<span class="name">entry</span> <span class="attr">key</span>=<span class="string">"namespace"</span>&gt;</span>pages<span class="tag">&lt;/<span class="name">entry</span>&gt;</span></span></div><div class="line"><span class="xml"></span></div><div class="line"><span class="xml">    <span class="tag">&lt;<span class="name">entry</span> <span class="attr">key</span>=<span class="string">"outRoot"</span>&gt;</span>D://generator-output<span class="tag">&lt;/<span class="name">entry</span>&gt;</span></span></div><div class="line"><span class="xml"></span></div><div class="line"><span class="xml">    <span class="comment">&lt;!-- 需要移除的表名前缀,使用逗号进行分隔多个前缀,示例值: t_,v_ --&gt;</span></span></div><div class="line"><span class="xml">    <span class="tag">&lt;<span class="name">entry</span> <span class="attr">key</span>=<span class="string">"tableRemovePrefixes"</span>&gt;</span>upms_<span class="tag">&lt;/<span class="name">entry</span>&gt;</span></span></div><div class="line"><span class="xml"></span></div><div class="line"><span class="xml">    <span class="tag">&lt;<span class="name">entry</span> <span class="attr">key</span>=<span class="string">"jdbc_username"</span>&gt;</span>root<span class="tag">&lt;/<span class="name">entry</span>&gt;</span></span></div><div class="line"><span class="xml">    <span class="tag">&lt;<span class="name">entry</span> <span class="attr">key</span>=<span class="string">"jdbc_password"</span>&gt;</span>root<span class="tag">&lt;/<span class="name">entry</span>&gt;</span></span></div><div class="line"><span class="xml"></span></div><div class="line"><span class="xml">    <span class="comment">&lt;!-- oracle需要指定jdbc.schema,其它数据库忽略此项配置  --&gt;</span></span></div><div class="line"><span class="xml">    <span class="tag">&lt;<span class="name">entry</span> <span class="attr">key</span>=<span class="string">"jdbc_schema"</span>&gt;</span><span class="tag">&lt;/<span class="name">entry</span>&gt;</span></span></div><div class="line"><span class="xml">    <span class="tag">&lt;<span class="name">entry</span> <span class="attr">key</span>=<span class="string">"jdbc_catalog"</span>&gt;</span><span class="tag">&lt;/<span class="name">entry</span>&gt;</span></span></div><div class="line"><span class="xml"></span></div><div class="line"><span class="xml">    <span class="comment">&lt;!-- 数据库类型映射  --&gt;</span></span></div><div class="line"><span class="xml">    <span class="tag">&lt;<span class="name">entry</span> <span class="attr">key</span>=<span class="string">"java_typemapping.java.sql.Timestamp"</span>&gt;</span>java.util.Date<span class="tag">&lt;/<span class="name">entry</span>&gt;</span></span></div><div class="line"><span class="xml">    <span class="tag">&lt;<span class="name">entry</span> <span class="attr">key</span>=<span class="string">"java_typemapping.java.sql.Date"</span>&gt;</span>java.util.Date<span class="tag">&lt;/<span class="name">entry</span>&gt;</span></span></div><div class="line"><span class="xml">    <span class="tag">&lt;<span class="name">entry</span> <span class="attr">key</span>=<span class="string">"java_typemapping.java.sql.Time"</span>&gt;</span>java.util.Date<span class="tag">&lt;/<span class="name">entry</span>&gt;</span></span></div><div class="line"><span class="xml">    <span class="tag">&lt;<span class="name">entry</span> <span class="attr">key</span>=<span class="string">"java_typemapping.java.lang.Byte"</span>&gt;</span>Integer<span class="tag">&lt;/<span class="name">entry</span>&gt;</span></span></div><div class="line"><span class="xml">    <span class="tag">&lt;<span class="name">entry</span> <span class="attr">key</span>=<span class="string">"java_typemapping.java.lang.Short"</span>&gt;</span>Integer<span class="tag">&lt;/<span class="name">entry</span>&gt;</span></span></div><div class="line"><span class="xml">    <span class="tag">&lt;<span class="name">entry</span> <span class="attr">key</span>=<span class="string">"java_typemapping.java.math.BigDecimal"</span>&gt;</span>Long<span class="tag">&lt;/<span class="name">entry</span>&gt;</span></span></div><div class="line"><span class="xml"></span></div><div class="line"><span class="xml">    <span class="comment">&lt;!-- H2 --&gt;</span></span></div><div class="line"><span class="xml">    <span class="comment">&lt;!-- &lt;entry key="jdbc.url"&gt;jdbc:h2:tcp://localhost/test&lt;/entry&gt;</span></span></div><div class="line"><span class="xml">    <span class="tag">&lt;<span class="name">entry</span> <span class="attr">key</span>=<span class="string">"jdbc.driver"</span>&gt;</span>org.h2.Driver<span class="tag">&lt;/<span class="name">entry</span>&gt;</span></span></div><div class="line"><span class="xml">    --&gt;</span></div><div class="line"><span class="xml"></span></div><div class="line"><span class="xml">    <span class="tag">&lt;<span class="name">entry</span> <span class="attr">key</span>=<span class="string">"jdbc_url"</span>&gt;</span>jdbc:mysql://localhost:3306/largh<span class="tag">&lt;/<span class="name">entry</span>&gt;</span></span></div><div class="line"><span class="xml">    <span class="tag">&lt;<span class="name">entry</span> <span class="attr">key</span>=<span class="string">"jdbc_driver"</span>&gt;</span>com.mysql.jdbc.Driver<span class="tag">&lt;/<span class="name">entry</span>&gt;</span></span></div><div class="line"><span class="xml"></span></div><div class="line"><span class="xml">    <span class="comment">&lt;!-- Oracle 	jdbc:oracle:oci:@tnsname_devdb</span></span></div><div class="line"><span class="xml">    <span class="tag">&lt;<span class="name">entry</span> <span class="attr">key</span>=<span class="string">"jdbc.url"</span>&gt;</span>jdbc:oracle:thin:@192.168.0.177:1521:[sid]<span class="tag">&lt;/<span class="name">entry</span>&gt;</span></span></div><div class="line"><span class="xml">    <span class="tag">&lt;<span class="name">entry</span> <span class="attr">key</span>=<span class="string">"jdbc.driver"</span>&gt;</span>oracle.jdbc.driver.OracleDriver<span class="tag">&lt;/<span class="name">entry</span>&gt;</span></span></div><div class="line"><span class="xml">    --&gt;</span></div><div class="line"><span class="xml"></span></div><div class="line"><span class="xml">    <span class="comment">&lt;!-- SQLServer2000</span></span></div><div class="line"><span class="xml">    <span class="tag">&lt;<span class="name">entry</span> <span class="attr">key</span>=<span class="string">"jdbc.url"</span>&gt;</span>jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=[database]<span class="tag">&lt;/<span class="name">entry</span>&gt;</span></span></div><div class="line"><span class="xml">    <span class="tag">&lt;<span class="name">entry</span> <span class="attr">key</span>=<span class="string">"jdbc.driver"</span>&gt;</span>com.microsoft.jdbc.sqlserver.SQLServerDriver<span class="tag">&lt;/<span class="name">entry</span>&gt;</span></span></div><div class="line"><span class="xml">    --&gt;</span></div><div class="line"><span class="xml"></span></div><div class="line"><span class="xml">    <span class="comment">&lt;!-- SQLServer2005</span></span></div><div class="line"><span class="xml">    <span class="tag">&lt;<span class="name">entry</span> <span class="attr">key</span>=<span class="string">"jdbc.url"</span>&gt;</span>jdbc:sqlserver://192.168.0.98:1433;DatabaseName=[database]<span class="tag">&lt;/<span class="name">entry</span>&gt;</span></span></div><div class="line"><span class="xml">    <span class="tag">&lt;<span class="name">entry</span> <span class="attr">key</span>=<span class="string">"jdbc.driver"</span>&gt;</span>com.microsoft.sqlserver.jdbc.SQLServerDriver<span class="tag">&lt;/<span class="name">entry</span>&gt;</span></span></div><div class="line"><span class="xml">    --&gt;</span></div><div class="line"><span class="xml"></span></div><div class="line"><span class="xml">    <span class="comment">&lt;!-- JTDs for SQLServer</span></span></div><div class="line"><span class="xml">    <span class="tag">&lt;<span class="name">entry</span> <span class="attr">key</span>=<span class="string">"jdbc.url"</span>&gt;</span>jdbc:jtds:sqlserver://192.168.0.102:1433/[database];tds=8.0;lastupdatecount=true<span class="tag">&lt;/<span class="name">entry</span>&gt;</span></span></div><div class="line"><span class="xml">    <span class="tag">&lt;<span class="name">entry</span> <span class="attr">key</span>=<span class="string">"jdbc.driver"</span>&gt;</span>net.sourceforge.jtds.jdbc.Driver<span class="tag">&lt;/<span class="name">entry</span>&gt;</span></span></div><div class="line"><span class="xml">    --&gt;</span></div><div class="line"><span class="xml"></span></div><div class="line"><span class="xml">    <span class="comment">&lt;!-- PostgreSql</span></span></div><div class="line"><span class="xml">   <span class="tag">&lt;<span class="name">entry</span> <span class="attr">key</span>=<span class="string">"jdbc.url"</span>&gt;</span>jdbc:postgresql://localhost/[database]<span class="tag">&lt;/<span class="name">entry</span>&gt;</span></span></div><div class="line"><span class="xml">   <span class="tag">&lt;<span class="name">entry</span> <span class="attr">key</span>=<span class="string">"jdbc.driver"</span>&gt;</span>org.postgresql.Driver<span class="tag">&lt;/<span class="name">entry</span>&gt;</span></span></div><div class="line"><span class="xml">    --&gt;</span></div><div class="line"><span class="xml"></span></div><div class="line"><span class="xml">    <span class="comment">&lt;!-- Sybase</span></span></div><div class="line"><span class="xml">   <span class="tag">&lt;<span class="name">entry</span> <span class="attr">key</span>=<span class="string">"jdbc.url"</span>&gt;</span>jdbc:sybase:Tds:localhost:5007/[database]<span class="tag">&lt;/<span class="name">entry</span>&gt;</span></span></div><div class="line"><span class="xml">   <span class="tag">&lt;<span class="name">entry</span> <span class="attr">key</span>=<span class="string">"jdbc.driver"</span>&gt;</span>com.sybase.jdbc.SybDriver<span class="tag">&lt;/<span class="name">entry</span>&gt;</span></span></div><div class="line"><span class="xml">    --&gt;</span></div><div class="line"><span class="xml"></span></div><div class="line"><span class="xml">    <span class="comment">&lt;!-- DB2</span></span></div><div class="line"><span class="xml">    <span class="tag">&lt;<span class="name">entry</span> <span class="attr">key</span>=<span class="string">"jdbc.url"</span>&gt;</span>jdbc:db2://localhost:5000/[database]<span class="tag">&lt;/<span class="name">entry</span>&gt;</span></span></div><div class="line"><span class="xml">    <span class="tag">&lt;<span class="name">entry</span> <span class="attr">key</span>=<span class="string">"jdbc.driver"</span>&gt;</span>com.ibm.db2.jdbc.app.DB2Driver<span class="tag">&lt;/<span class="name">entry</span>&gt;</span></span></div><div class="line"><span class="xml">    --&gt;</span></div><div class="line"><span class="xml"></span></div><div class="line"><span class="xml">    <span class="comment">&lt;!-- HsqlDB</span></span></div><div class="line"><span class="xml">    <span class="tag">&lt;<span class="name">entry</span> <span class="attr">key</span>=<span class="string">"jdbc.url"</span>&gt;</span>jdbc:hsqldb:mem:generatorDB<span class="tag">&lt;/<span class="name">entry</span>&gt;</span></span></div><div class="line"><span class="xml">    <span class="tag">&lt;<span class="name">entry</span> <span class="attr">key</span>=<span class="string">"jdbc.driver"</span>&gt;</span>org.hsqldb.jdbcDriver<span class="tag">&lt;/<span class="name">entry</span>&gt;</span></span></div><div class="line"><span class="xml">    --&gt;</span></div><div class="line"><span class="xml"></span></div><div class="line"><span class="xml">    <span class="comment">&lt;!-- Derby</span></span></div><div class="line"><span class="xml">    <span class="tag">&lt;<span class="name">entry</span> <span class="attr">key</span>=<span class="string">"jdbc.url"</span>&gt;</span>jdbc:derby://localhost/databaseName<span class="tag">&lt;/<span class="name">entry</span>&gt;</span></span></div><div class="line"><span class="xml">    <span class="tag">&lt;<span class="name">entry</span> <span class="attr">key</span>=<span class="string">"jdbc.driver"</span>&gt;</span>org.apache.derby.jdbc.ClientDriver<span class="tag">&lt;/<span class="name">entry</span>&gt;</span></span></div><div class="line"><span class="xml">    --&gt;</span></div><div class="line"><span class="xml"></span></div><div class="line"><span class="xml"><span class="tag">&lt;/<span class="name">properties</span>&gt;</span></span></div></pre></td></tr></table></figure></p>
<p>这个配置文件需要配置你数据库信息、basepackage（基础包路径）、outRoot（输出目录）、tableRemovePrefixes（移除表的前缀）等，仔细看看应该没什么问题。到这里直接上一张项目图：<br><img src="http://upload-images.jianshu.io/upload_images/6332391-1691bd65da4551c1.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/600" alt="图一"><br>PS：有些同学导入源码进去编译的时候可能会报错，原因是你的jdk版本是1.7或1.8，因为这个项目可能比较久了，作者写的时候用的是老一点版本，而高版本中有个类在1.7增加了一个方法。你只需要按照错误提示操作增加这个方法就行。<br><img src="http://upload-images.jianshu.io/upload_images/6332391-aec768ecdf319fc6.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/800" alt="图二"></p>
<h1 id="建立骨架项目"><a href="#建立骨架项目" class="headerlink" title="建立骨架项目"></a>建立骨架项目</h1><p>编写一个项目骨架模版，这样生成的代码可以直接运行跑起来。下面是我的项目骨架结构：<br><img src="http://upload-images.jianshu.io/upload_images/6332391-2c8a015f1893fdf2.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/600" alt="图三"><br>里面相关代码都是用代码生成器里面的变量来进行替换，更多变量需要查看源码里面几个model，根据这些就可以定制生成你的项目了。<br>新建一个生成代码的main方法，运行这个方法即可<br><figure class="highlight processing"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> main(<span class="keyword">String</span>[] args) <span class="keyword">throws</span> Exception &#123;</div><div class="line">    <span class="keyword">String</span> templatePath = <span class="string">"E:\\IdeaProjects\\largh-codegen\\src\\main\\resources\\template"</span>;</div><div class="line">    GeneratorFacade g = <span class="keyword">new</span> GeneratorFacade();</div><div class="line">    g.getGenerator().addTemplateRootDir(templatePath);</div><div class="line">    <span class="comment">//删除生成器的输出目录//</span></div><div class="line">    g.deleteOutRootDir();</div><div class="line">    g.generateByAllTable();</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<p>templatePath就是你template的完整路径，你放在哪里，就填写哪个。运行完这个方法可以看到我输出目录里面多了一个largh-api-quickstart文件夹，这个就是生成的项目了，接下来把生成的代码导入idea下跑起来<br><img src="http://upload-images.jianshu.io/upload_images/6332391-e454eb5b97ee392c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/600" alt="图四"><br><img src="http://upload-images.jianshu.io/upload_images/6332391-cc2e982285a8a649.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/600" alt="图五"><br><img src="http://upload-images.jianshu.io/upload_images/6332391-c41c4b2dfaec595d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/600" alt="图六"><br>你想只生成实体类这些，都是可以的，有兴趣的同学可以研究下源码。到这里基本就完成了，再也不怕面对枯燥的CRUD了。<br>本文源码地址：<strong><a href="https://github.com/itfuyun/largh-codegen" target="_blank" rel="external">largh-codegen</a></strong></p>

        
        </div>
		
		
            
<div>
  <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
    <div style="margin-bottom:10px">请作者喝杯咖啡吧！</div>
    <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
      <span>赏</span>
    </button>
    <div id="QR" style="display: none;">
        <div id="wechat" style="display: inline-block">
          <a href="/css/images/wechatpay.png" class="fancybox fancybox.image" rel="group"><img id="wechat_qr" src="/css/images/wechatpay.png" alt="过眼浮云 WeChat Pay"></a>
          <p>微信打赏</p>
        </div>
        <div id="alipay" style="display: inline-block">
          <a href="/css/images/alipay.png" class="fancybox fancybox.image" rel="group"><img id="alipay_qr" src="/css/images/alipay.png" alt="过眼浮云 Alipay"></a>
          <p>支付宝打赏</p>
        </div>
    </div>
  </div>
</div>

        
		
		
            <! -- 添加版权信息 -->
<div class="article-footer-copyright">
<div>本站文章除注明转载外，均为本站原创或编译。欢迎任何形式的转载，但请务必注明出处。</div>
<div>转载请注明：文章转载自 IT浮云 <a href="/index.html" target="_blank" title="IT浮云">[http://www.itfuyun.com]</a></div>
<div>本文标题：告别枯燥的CRUD，Java代码生成器的使用</div>
<div>本文地址：<a href="/posts/rapid-generator.html" target="_blank" title="告别枯燥的CRUD，Java代码生成器的使用">http://www.itfuyun.com/posts/rapid-generator.html</a></div>
</div>
<! -- 添加版权信息 -->
        
		
        <footer class="article-footer">
            <div class="share-container">


    <div class="bdsharebuttonbox">
    <a href="#" class="bds_more" data-cmd="more">分享到：</a>
    <a href="#" class="bds_qzone" data-cmd="qzone" title="分享到QQ空间">QQ空间</a>
    <a href="#" class="bds_tsina" data-cmd="tsina" title="分享到新浪微博">新浪微博</a>
    <a href="#" class="bds_tqq" data-cmd="tqq" title="分享到腾讯微博">腾讯微博</a>
    <a href="#" class="bds_renren" data-cmd="renren" title="分享到人人网">人人网</a>
    <a href="#" class="bds_weixin" data-cmd="weixin" title="分享到微信">微信</a>
</div>
<script>
window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{"bdSize":16}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];
</script>
<style>
    .bdshare_popup_box {
        border-radius: 4px;
        border: #e1e1e1 solid 1px;
    }
    .bdshare-button-style0-16 a,
    .bdshare-button-style0-16 .bds_more {
        padding-left: 20px;
        margin: 6px 10px 6px 0;
    }
    .bdshare_dialog_list a,
    .bdshare_popup_list a,
    .bdshare_popup_bottom a {
        font-family: 'Microsoft Yahei';
    }
    .bdshare_popup_top {
        display: none;
    }
    .bdshare_popup_bottom {
        height: auto;
        padding: 5px;
    }
</style>


</div>

            
    
        <a href="http://www.itfuyun.com/posts/rapid-generator.html#comments" id="sourceId::posts/rapid-generator.html" class="article-comment-link cy_cmt_count">评论</a>
    

        </footer>
    </div>
    
        
<nav id="article-nav">
    
        <a href="/posts/java-cookie-utils.html" id="article-nav-newer" class="article-nav-link-wrap">
            <strong class="article-nav-caption">上一篇</strong>
            <div class="article-nav-title">
                
                    Java操作Cookie工具类
                
            </div>
        </a>
    
    
        <a href="/posts/hexo-learn.html" id="article-nav-older" class="article-nav-link-wrap">
            <strong class="article-nav-caption">下一篇</strong>
            <div class="article-nav-title">Hexo 常用命令及操作</div>
        </a>
    
</nav>


    
</article>


    
    
        <section id="comments">
    <div id="SOHUCS" sid="posts/rapid-generator.html"></div>
</section>
    

</section>
            
                
<aside id="sidebar">
   
        
    <div class="widget-wrap">
        <h3 class="widget-title">最新文章</h3>
        <div class="widget">
            <ul id="recent-post" class="no-thumbnail">
                
                    <li>
                        
                        <div class="item-inner">
                            <p class="item-category"><a class="article-category-link" href="/categories/design-pattern/">设计模式</a></p>
                            <p class="item-title"><a href="/posts/design-pattern-lsp.html" class="title">设计模式之-里氏替换原则LSP</a></p>
                            <p class="item-date"><time datetime="2017-10-20T05:50:53.000Z" itemprop="datePublished">2017-10-20</time></p>
                        </div>
                    </li>
                
                    <li>
                        
                        <div class="item-inner">
                            <p class="item-category"><a class="article-category-link" href="/categories/programming/">编程</a></p>
                            <p class="item-title"><a href="/posts/freemarker-generates-static-files.html" class="title">Freemarker生成静态文件</a></p>
                            <p class="item-date"><time datetime="2017-10-16T07:58:51.000Z" itemprop="datePublished">2017-10-16</time></p>
                        </div>
                    </li>
                
                    <li>
                        
                        <div class="item-inner">
                            <p class="item-category"><a class="article-category-link" href="/categories/source-code-analysis/">源码分析</a></p>
                            <p class="item-title"><a href="/posts/hashmap-source-code-analysis.html" class="title">HashMap 源码分析</a></p>
                            <p class="item-date"><time datetime="2017-10-14T06:31:55.000Z" itemprop="datePublished">2017-10-14</time></p>
                        </div>
                    </li>
                
                    <li>
                        
                        <div class="item-inner">
                            <p class="item-category"><a class="article-category-link" href="/categories/programming/">编程</a></p>
                            <p class="item-title"><a href="/posts/java-cookie-utils.html" class="title">Java操作Cookie工具类</a></p>
                            <p class="item-date"><time datetime="2017-09-27T14:53:34.000Z" itemprop="datePublished">2017-09-27</time></p>
                        </div>
                    </li>
                
                    <li>
                        
                        <div class="item-inner">
                            <p class="item-category"><a class="article-category-link" href="/categories/programming/">编程</a></p>
                            <p class="item-title"><a href="/posts/rapid-generator.html" class="title">告别枯燥的CRUD，Java代码生成器的使用</a></p>
                            <p class="item-date"><time datetime="2017-09-26T12:49:39.000Z" itemprop="datePublished">2017-09-26</time></p>
                        </div>
                    </li>
                
            </ul>
        </div>
    </div>

    
        
    <div class="widget-wrap">
        <h3 class="widget-title">分类</h3>
        <div class="widget">
            <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/categories/other/">其他</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/source-code-analysis/">源码分析</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/programming/">编程</a><span class="category-list-count">3</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/design-pattern/">设计模式</a><span class="category-list-count">1</span></li></ul>
        </div>
    </div>

    
        
    <div class="widget-wrap">
        <h3 class="widget-title">归档</h3>
        <div class="widget">
            <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/10/">十月 2017</a><span class="archive-list-count">3</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/09/">九月 2017</a><span class="archive-list-count">3</span></li></ul>
        </div>
    </div>

    
        
    <div class="widget-wrap">
        <h3 class="widget-title">标签云</h3>
        <div class="widget tagcloud">
            <a href="/tags/cookie/" style="font-size: 10px;">-cookie</a> <a href="/tags/hexo/" style="font-size: 10px;">-hexo</a> <a href="/tags/代码生成/" style="font-size: 10px;">-代码生成</a> <a href="/tags/HashMap/" style="font-size: 10px;">HashMap</a> <a href="/tags/freemarker/" style="font-size: 10px;">freemarker</a> <a href="/tags/lsp/" style="font-size: 10px;">lsp</a> <a href="/tags/里氏替换/" style="font-size: 10px;">里氏替换</a>
        </div>
    </div>

    
    <div id="toTop" class="fa fa-angle-up"></div>
</aside>

            
        </div>
        <footer id="footer">
    <div class="outer">
        <div id="footer-info" class="inner">
            &copy; 2018 IT浮云<br>
            Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>. Theme by <a href="http://github.com/ppoffice">PPOffice</a>
        </div>
    </div>
</footer>
        
    
    <script id="cy_cmt_num" src="https://changyan.sohu.com/upload/plugins/plugins.list.count.js?clientId=cytezlr4l"></script>
    <script charset="utf-8" type="text/javascript" src="https://changyan.sohu.com/upload/changyan.js" ></script>
    <script type="text/javascript">
    window.changyan.api.config({
    appid: 'cytezlr4l',
    conf: '7625b051042273ad10802550abd3d130'
    });
    </script>




    
        <script src="/libs/lightgallery/js/lightgallery.min.js"></script>
        <script src="/libs/lightgallery/js/lg-thumbnail.min.js"></script>
        <script src="/libs/lightgallery/js/lg-pager.min.js"></script>
        <script src="/libs/lightgallery/js/lg-autoplay.min.js"></script>
        <script src="/libs/lightgallery/js/lg-fullscreen.min.js"></script>
        <script src="/libs/lightgallery/js/lg-zoom.min.js"></script>
        <script src="/libs/lightgallery/js/lg-hash.min.js"></script>
        <script src="/libs/lightgallery/js/lg-share.min.js"></script>
        <script src="/libs/lightgallery/js/lg-video.min.js"></script>
    
    
        <script src="/libs/justified-gallery/jquery.justifiedGallery.min.js"></script>
    
    



<!-- Custom Scripts -->
<script src="/js/main.js"></script>

    </div>
</body>
</html>